package com.longdaji.decoration.ui.comment;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.GoodsCommentListBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class CriticismPresenter extends RxPresenter<CriticismContract.View> implements CriticismContract.Presenter {
    private final static String TAG = CriticismPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public CriticismPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getCommentList(String goodId, String userId) {
        addSubscribe(mDataManager.getGoodsComments(goodId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsCommentListBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsCommentListBean>() {
                    @Override
                    public void onSuccess(GoodsCommentListBean goodsCommentListBean) {

                        if (mView != null) {
                            Log.d(TAG, "GoodsCommentListBean is" + goodsCommentListBean);
                            mView.showCommentList(goodsCommentListBean.getCommentGoods());
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast("获取评论信息失败");
                            mView.showCommentListNone();
                        }
                    }
                }));
    }

    @Override
    public void likeOrDislikeGoods(String userId, String commentId, final String isLike, final int position) {
        addSubscribe(mDataManager.likeGoodsOrNot(userId, commentId, isLike)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {

                        if (mView != null) {
                            Log.d(TAG, "likeOrDislikeGoods is" + httpResponse);
                            if ("1".equals(isLike)) {
                                if ("0".equals(httpResponse.getStatusCode())) {
                                    mView.showLikeSuccess(position);
                                } else {
                                    mView.showLikeFail();
                                }
                            } else {
                                if ("0".equals(httpResponse.getStatusCode())) {
                                    mView.showDisLikeSuccess(position);
                                } else {
                                    mView.showDisLikeFail();
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }
}
